package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberMapper;
import com.itheima.health.dao.OrderMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@DubboService
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    OrderMapper orderMapper;

    /**
     * @Author: JASON
     * @Date: 10:17 2021/1/16
     * @Parms []
     * @ReturnType: java.util.Map<java.lang.String,java.lang.Object>
     * @Description: 获取运营数据
    */
    @Override
    public Map<String, Object> getBussinessReport() {
        /**
         * 获取运营统计数据
         * Map数据格式：
         *      reportDate -> yyyy-MM-dd        //日期
         *      todayNewMember -> number		// 今日新增会员
         *      thisWeekNewMember -> number		// 本周新增会员
         *      thisMonthNewMember -> number	// 本月新增会员
         *      totalMember -> number			//  总会员
         *      todayOrderNumber -> number		// 今日预约人数
         *      thisWeekOrderNumber -> number	// 本周预约人数
         *      thisMonthOrderNumber -> number	// 本月预约人数
         *      todayVisitsNumber -> number		// 今日到诊人数
         *      thisWeekVisitsNumber -> number	// 本周到诊人数
         *      thisMonthVisitsNumber -> number	// 本月到诊人数
         *      hotSetmeal ->  [ {"proportion":0.4545,"name":"粉红珍爱(女)升级TM12项筛查体检套餐","setmeal_count":5}]	// 热门套餐
         * @return
         */
        HashMap<String, Object> result = new HashMap<>();
        //日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        result.put("reportDate", sdf.format(new Date()));
        //今天
        Calendar today= Calendar.getInstance();
        //时分秒清零
        today.set(Calendar.HOUR_OF_DAY,0);
        today.set(Calendar.MINUTE,0);
        today.set(Calendar.SECOND,0);
        today.set(Calendar.MILLISECOND,0);
        //设置周一作为每周第一天
        today.setFirstDayOfWeek(Calendar.MONDAY);
        //本周第一天
        Calendar weekFristDay = (Calendar) today.clone();
        weekFristDay.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        //本周最后一天
        Calendar weeklastDay = (Calendar) today.clone();
        weekFristDay.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
        //本月第一天
        Calendar monthFristDay = (Calendar) today.clone();
        monthFristDay.set(Calendar.DAY_OF_MONTH,1);
        //本月最后一天
        Calendar monthLastDay = (Calendar) today.clone();
        //获取当前月最大的天数
        int maxDayofMonth = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置本月的最后一天
        monthLastDay.set(Calendar.DAY_OF_MONTH, maxDayofMonth);
        //会员相关
        //今天
        result.put("todayNewMember", memberMapper.countByRegTimeRange(today.getTime(),today.getTime()));
        //本周
        result.put("thisWeekNewMember", memberMapper.countByRegTimeRange(weekFristDay.getTime(), weeklastDay.getTime()));
        //本月
        result.put("thisMonthNewMember", memberMapper.countByRegTimeRange(monthFristDay.getTime(), monthLastDay.getTime()));
        //会员总数
        result.put("totalMember", memberMapper.countByRegTimeBefore(sdf.format(today.getTime())));

        //预约相关
        result.put("todayOrderNumber", orderMapper.countByOrderDateRange(today.getTime(), today.getTime(),null));
        result.put("thisWeekOrderNumber", orderMapper.countByOrderDateRange(weekFristDay.getTime(), weeklastDay.getTime(),null));
        result.put("thisMonthOrderNumber", orderMapper.countByOrderDateRange(monthFristDay.getTime(), monthLastDay.getTime(),null));
        result.put("todayVisitsNumber", orderMapper.countByOrderDateRange(today.getTime(), today.getTime(), Order.ORDERSTATUS_YES));
        result.put("thisWeekVisitsNumber", orderMapper.countByOrderDateRange(weekFristDay.getTime(), weeklastDay.getTime(), Order.ORDERSTATUS_YES));
        result.put("thisMonthVisitsNumber", orderMapper.countByOrderDateRange(monthFristDay.getTime(), monthLastDay.getTime(), Order.ORDERSTATUS_YES));

        //热门套餐
        result.put("hotSetmeal", orderMapper.countByHotSetmeal(1));



        return result;
    }
}
